package com.threeonemeeting.activity;

import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.task.SendMessageTask;

public class SendMessageActivity extends BaseTabActivity implements ICallBack {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_send_message);
		init("写站内信");

		// 帐号编辑框
		final EditText editReceiverName = (EditText) findViewById(R.id.editReceiverName);
		editReceiverName.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_input_message_receiver);
				String strReceiverName = editReceiverName.getText().toString();
				if (hasFocus == true) {
					if (strReceiverName.equalsIgnoreCase(strHint) == true) {
						editReceiverName.setText("");
					}
				} else {

					if (strReceiverName.equalsIgnoreCase("") == true) {
						editReceiverName.setText(strHint);
					}
				}
			}
		});

		Button btnSubmit = (Button) findViewById(R.id.btn_submit);
		btnSubmit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				String strHint = getResources().getString(
						R.string.string_input_message_receiver);

				String strReceiverName = editReceiverName.getText().toString();
				if (MiscHelper.isEmpty(strReceiverName) == true
						|| strReceiverName.equalsIgnoreCase(strHint) == true) {
					MessageBox.showToast(getApplicationContext(), "收件人昵称必须输入");
					return;
				}

				EditText editMessageContent = (EditText) findViewById(R.id.editMessageContent);
				String strMessageContent = editMessageContent.getText()
						.toString();
				if (MiscHelper.isEmpty(strMessageContent) == true) {
					MessageBox.showToast(getApplicationContext(), "说点什么吧");
					return;
				}

				SendMessageTask task = new SendMessageTask(
						SendMessageActivity.this, SendMessageActivity.this);
				Bundle params = new Bundle();
				params.putString(
						"uid",
						String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
				params.putString(
						"pass",
						DataCenter.getInstance().currentLoginUserInfo.strPassword);
				params.putString("name", strReceiverName);
				params.putString("content", strMessageContent);
				TaskManager.addTask(task, params, false);
			}
		});
	}

	public void doCallBack(Map<String, Object> mapData) {
		Integer msgType = (Integer) mapData.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			Log.d(Const.Tag, "can't find type in response type");
			return;
		}

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			// 报名失败
			if (msgType == HttpConst.TASKTYPE_SENDMESSAGE) {

				String strErrorMsg = (String) mapData.get("errmsg");
				if (strErrorMsg != null)
					MessageBox.showToast(this, strErrorMsg);
				
				return;
			}
		}

		// 获取热门的会议列表
		if (msgType == HttpConst.TASKTYPE_SENDMESSAGE) {
			Boolean bSuccess = (Boolean) mapData.get("result");
			if (bSuccess == null) {
				MessageBox.showToast(getApplicationContext(), "站内信发送失败");
				return;
			}

			MessageBox.showToast(getApplicationContext(), "站内信发送成功");

		}
	}
}
